package com.ikun.common.manager.fileHandler.impl;

import com.ikun.common.constant.LogPrefixConstant;
import com.ikun.common.manager.fileHandler.FileManger;
import com.ikun.common.result.Result;
import com.ikun.common.utils.AliOssCloudUtils;
import com.ikun.common.utils.RandomStr;
import com.ikun.common.utils.SystemUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName FileMangerImpl  //类名称
 * @Description: 类描述
 * @Author: ximeng    //作者
 * @CreateDate: 2024/1/6 16:16	//创建时间
 * @UpdateUser: 更新人
 * @UpdateDate: 2024/1/6 16:16	//更新时间
 * @UpdateRemark: 更新的信息
 * @Version: 1.0    //版本号
 */
@Service
@Slf4j
public class FileMangerImpl implements FileManger {
    private static final Logger log = LoggerFactory.getLogger(FileMangerImpl.class);
    @Value("${aliyun.oss.accessKeyId:null}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret:null}")
    private String accessKeySecret;


    @Override
    public Result<List<String>> uploadFileList(List<MultipartFile> fileList) throws IOException {

        List<String> fileUrlList = new ArrayList<>();
        //1、获取文件名截取出后缀
        for (MultipartFile file : fileList) {
            String imageUrl = this.upload(file);
            fileUrlList.add(imageUrl);
        }
        //4、返回云地址
        return new Result<List<String>>().success(fileUrlList);
    }

    @Override
    public Result<String> uploadFile(MultipartFile file) throws IOException {
        return new Result<String>().success(this.upload(file));
    }

    private String upload(MultipartFile file) throws IOException {
        String suffix = "";
        String[] fileNameParts = file.getOriginalFilename().split("\\.");
        if (fileNameParts.length > 1) {
            suffix = fileNameParts[fileNameParts.length - 1];
        }
        //2、生成随机文件名拼接上后缀
        String fileName = RandomStr.generateRandomString(15) + "." + suffix;

        String imageUrl = null;

        if (Objects.isNull(accessKeyId) || Objects.isNull(accessKeySecret)) {
            log.info("");
            //使用本地储存
            String currentDir = SystemUtils.getCurrentWorkDir();
            String path = currentDir + File.separator + "myImages"; // 使用File.separator以确保跨平台兼容性
            File file1 = new File(path);
            if (!file1.exists()) {
                file1.mkdirs();
            }
            imageUrl = path + "/" + fileName;
            File file2 = new File(imageUrl);
            file2.createNewFile();
        } else {
            //3、调用oss云存储村储存数据
            AliOssCloudUtils aliOssCloudUtils = new AliOssCloudUtils();
            imageUrl = aliOssCloudUtils.uploadFile2OSS(file.getInputStream(), fileName);

        }
        log.info(LogPrefixConstant.LOG_PREFIX_OSS_UPLOAD + "文件上传成功");
        return imageUrl;
    }
}
